(function(){
'use strict';
/**
 * customer 吴尧
 * @ngdoc function
 * @name sasdApp.controller:customerAnalysisController
 * @description 客户发展分析控制器
 * # customerAnalysisController
 * Controller of the sasApp
 */
angular.module('controllers.oldOMDisplay.infoDispaly')
  .controller('customerAnalysisCtrl', customerAnalysisCtrl);

function customerAnalysisCtrl($scope ,CoreService,InfoViewService,$filter,COREDATA,CoreLoading) {
  $scope.errorInfo = COREDATA.ERRORINFO;   //日期错误信息
  $scope.provinces = COREDATA.PROVINCES;
	var paramsData = {};
  $scope.aaa = false;
  $scope.currentPage = 1;
  $scope.pageSize = 10;

  $scope.pageItmes = [10, 20, 30, 40, 50, 60];

  $scope.shoumun = 1;
  //前台模拟数据
  $scope.viewTypes = [{
    viewType: '1',
    viewTypeName: '分量'
  }, {
    viewType: '2',
    viewTypeName: '总量'
  }];
  $scope.searchInfo = {
    startTime: new Date(new Date().setMonth(new Date().getMonth() - 7)),
    endTime: new Date(new Date().setMonth(new Date().getMonth() - 1)),
    provinceCode: [],
    viewType: '1',
    businessCode: '01'
  };

  $scope.queryData = function(){

    $scope.shoumun==1?$scope.shoumun=0:$scope.shoumun=1;
    paramsData.provinceCodes = $scope.searchInfo.provinceCode.map(function(item){
      return item.provinceCode;
    }); 
    $scope.searchParam = angular.copy($scope.searchInfo);
    $scope.searchParam.businessCode = $scope.searchInfo.viewType === '2' ? '' : $scope.searchInfo.businessCode;
    $scope.searchParam.startTime = CoreService.dateFormat($scope.searchParam.startTime, 'yyyyMM');
    $scope.searchParam.endTime =  CoreService.dateFormat($scope.searchParam.endTime, 'yyyyMM');
	  paramsData.startTime = $filter('date')($scope.searchInfo.startTime, 'yyyy-MM');
	  paramsData.endTime = $filter('date')($scope.searchInfo.endTime, 'yyyy-MM');
    if($scope.shoumun === 0){

      CoreLoading.setState(true);
      if($scope.aaa === false){      
        $scope.queryCustomer();
      }else {
        $scope.queryPageCustomerFormfunction();
      } 
    }
  };

  //sysCode统一支付的业务请求
  var sysCode = {
    systemCode: '11'
  };

  function queryBusiness() {
    InfoViewService.queryBusiness(sysCode,function(res){
      if(res.state === '10000'){
        $scope.flagObj = true;
        $scope.businessList = res.data;
      }else{
        CoreService.dialog('请求数据失败');
      }
    });
  }
  queryBusiness();
  
  

  var title, arrTime, arrArrive, arrActive;

  $scope.queryCustomer = function() {
    CoreLoading.setState(true);
   
    InfoViewService.queryCustomer(paramsData, function(res){
      if(res.state === '10000'){
         CoreLoading.setState(false);
        if(res.data && res.data.length > 0 ){

          arrTime = [];
          arrArrive = [];
          arrActive = [];

          angular.forEach(res.data, function(item){
            arrTime.push(item.time);
            arrArrive.push(item.customerArrived);
            arrActive.push(item.customerActive);
          });
          angular.forEach($scope.provinces, function(province){
            if($scope.searchInfo.provinceCode === province.provinceCode){
              $scope.provinceName = province.provinceName;
            }
          });
          //省份多选
          $scope.provinceName = $scope.searchInfo.provinceCode.length === 0 ? '全国': $scope.searchInfo.provinceCode.map(function(item){
            return item.provinceName;
          }).join('、');
          //省份单选
          // !$scope.provinceName && ($scope.provinceName = '全国');

          title = $scope.searchParam.startTime + '_' + $scope.searchParam.endTime +$scope.provinceName + '客户到达数与活跃客户数统计';
          if (document.getElementById('customer')) {

            var customerEchart = echarts.init(document.getElementById('customer'));

            var options = {
              title: { text: title, x: 'center'},
              grid: { x2: '10%'},
              tooltip: { trigger: 'axis'},
              legend: { orient: 'vertical', x: 'right', y: 'center', data: ['客户到达数', '客户活跃数']},
              dataZoom : {show : true,realtime : true,start : 0,end : 100},
              xAxis: [{type: 'category',data: arrTime}],
              yAxis: [{name: '(人数)',type: 'value'}],
              series: [
              { name: '客户到达数', type: 'line',  data: arrArrive}, 
              { name: '客户活跃数',type: 'line',data: arrActive}
              ]
            };

            customerEchart.setOption(options);
            $scope.flag ='1';
          }
        }else{
           $scope.flag = 'hiddenChart';  //没有数据时隐藏
           CoreService.dialog('当前无数据');
        }

      }else{
        CoreService.dialog(res.state);
      }
    });

  };


  /**
   * [queryPageCustomerForm 查询表数据]
   * @return {[type]} [description]
   */
  $scope.queryPageCustomerFormfunction = function() {
    CoreLoading.setState(true);
    $scope.queryCustomerForm($scope.currentPage,$scope.pageSize);
  };


  $scope.queryCustomerForm = function(currentPage,pageSize) {
    CoreLoading.setState(true);
    var page = {currentPage:currentPage,pageSize:pageSize};

    $scope.currentPage = currentPage;
    $scope.searchParam.page = page;
    InfoViewService.queryCustomerForm( paramsData, queryCustomerForm);
  };

  $scope.queryData('searchButton');

  /**
   * [queryCustomerForm 查询表格数据]
   * @param  {[type]} res [后台返回数据]
   */
  function queryCustomerForm(res) {
    CoreLoading.setState(false);
    if(res.state === '10000'){
       $scope.formData = res.data;
       $scope.totalCount = res.data.totalCount;
    }else{
      CoreService.dialog(res.state);
    }
   
  }



}

})();
